<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <!-- 表单 -->
    <el-form ref="addEmployee" label-width="120px" :model="formdata" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formdata.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formdata.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formdata.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment" @blur="checkFn">
        <el-select v-model="formdata.formOfEmployment" style="width:50%" placeholder="请选择">
          <!-- 遍历只能遍历组件的数据 -->
          <el-option v-for="item in EmployeeEnum" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formdata.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formdata.departmentName" style="width:50%" placeholder="请选择部门" @focus="getDapartments" />
        <!-- 放置一个tree组件 -->
        <el-tree
          v-if="showTree"
          v-loading="loading"
          class="departmentTree"
          :data="treeData"
          default-expand-all=""
          :props="{ label: 'name' }"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formdata.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
// 引入获取部门数据接口
import { getDapartments } from '@/api/departments'
// 引入将数组转换位树形函数
import { listToTreeData } from '@/utils/index'
import EmployeeEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/emploees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum: EmployeeEnum.hireType,
      // 定义数组接收到树形数据
      treeData: [],
      showTree: false,
      loading: false,
      formdata: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }, { min: 1, max: 4, message: '用户姓名为1到4位', trigger: 'blur' }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不对', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职事件不能为空', trigger: 'blur' }],
        correctionTime: [{ required: true, message: '转正时间不能为空', trigger: 'blur' }]
      }
    }
  },

  methods: {
    async getDapartments() {
      //   获取部门数据
      const { depts } = await getDapartments()
      console.log(79, depts)
      //   显示树形部门组件
      this.showTree = true
      this.loading = true
      //   将数组转换位树形
      this.treeData = listToTreeData(depts, '')
      this.loading = false
    },

    // 点击部门触发，（选完部门后，关闭树形）
    selectNode(node) {
    //   console.log(103, node)
    // 将选中部门赋值给渲染数据
      this.formdata.departmentName = node.name
      // 关闭树形结构
      this.showTree = false
    },

    // 检查表单部分结构
    checkFn() {
      setTimeout(() => {
        this.$refs.addEmployee.validateField('formOfEmployment')
      }, 100)
    },

    // 点击确定
    async btnOk() {
      // 先校验
      console.log(126, this.formdata)
      await this.$refs.addEmployee.validate()
      console.log(1)
      // 调用新增接口函数
      await addEmployee(this.formdata)
      // 提示用户
      this.$message.success('新增成功')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 清空表单内容
      this.$refs.addEmployee.resetFields()
      // 告诉父组件更新数据
      this.$emit('addEmployee')
    },

    // 点击取消
    btnCancel() {
      // 清除formdata数据
      this.formdata = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 清除表单数据
      this.$refs.addEmployee.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    }

  }

}
</script>

<style lang="scss" scoped>
.departmentTree{
  width: 100%;
  height: 200px;
  position: absolute;
  cursor: default;
  background-color: #fff;
  color: #606266;
  border: 1px solid #ccc;
  overflow: auto;
  z-index: 9;
}
</style>
